package com.example.module_park.enterprise

import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.lib_common.base.state.Empty
import com.example.lib_common.base.state.Failed
import com.example.lib_common.base.state.Loading
import com.example.lib_common.base.state.RequestEvent
import com.example.lib_common.base.state.Success
import com.example.module_park.model.ApplicationRequest
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class EnterpriseApplicationViewModel @Inject constructor(
    private val repository: EnterpriseRepository
) : ViewModel() {

    // 表单数据
    val companyName = MutableStateFlow("")
    val contactName = MutableStateFlow("")
    val phone = MutableStateFlow("")
    val entryDate = MutableStateFlow("")
    val area = MutableStateFlow("")
    val personCount = MutableStateFlow("")
    val attachments = MutableStateFlow<List<String>>(emptyList())

    // 提交状态
    private val _submitState = MutableStateFlow<RequestEvent>(Empty)
    val submitState: StateFlow<RequestEvent> = _submitState

    // 日期选择处理
    fun onDateSelected(year: Int, month: Int, dayOfMonth: Int) {
        val dateStr = String.format("%d-%02d-%02d", year, month, dayOfMonth)
        entryDate.value = dateStr
    }

    // 提交申请
    fun submitApplication() {

//        if (validateForm()) {
            Log.e("FANSS", "提交申请")
            viewModelScope.launch {
                _submitState.value = Loading

                repository.submitApplication(
                    ApplicationRequest(
                        name = companyName.value,
                        stockName = "智慧科技",
                        contactInfo = phone.value.toLong(),
                        legalRepresentative = contactName.value,
                        stockCode = 10010,
                        address = "智慧星球",
                        status = "运营中",
                        photos = attachments.value,
                        id = 0,
                        industryType = "智慧星球科技",
                        listed = true
                    )
                ).collect {
                    Log.e("FANSS", "提交申请成功")
                        _submitState.value = Success

                }
            }
//        }
    }

    // 添加附件
    fun addAttachment(url: String) {
        attachments.value = attachments.value + url
    }

    // 移除附件
    fun removeAttachment(url: String) {
        attachments.value = attachments.value.filterNot { it == url }
    }

    // 表单验证
    private fun validateForm(): Boolean {
        return when {
            companyName.value.isEmpty() -> {
                _submitState.value = Failed("请输入企业名称")
                false
            }
            contactName.value.isEmpty() -> {
                _submitState.value = Failed("请输入联系人姓名")
                false
            }
            !isValidPhone(phone.value) -> {
                _submitState.value = Failed("请输入有效的手机号码")
                false
            }
            entryDate.value.isEmpty() -> {
                _submitState.value = Failed("请选择入驻时间")
                false
            }
            area.value.toDoubleOrNull() == null -> {
                _submitState.value = Failed("请输入有效的申请面积")
                false
            }
            personCount.value.toIntOrNull() == null -> {
                _submitState.value = Failed("请输入有效的入驻人数")
                false
            }
            else -> true
        }
    }

    private fun isValidPhone(phone: String): Boolean {
        return phone.matches(Regex("^1[3-9]\\d{9}$"))
    }
}